$(function () {
    initDatagrid();
    // 带参数的数据请求
    $("#queryButton").click(function(){
        if($("#verifyStatus").combobox('getValue') == "-"){
            $("#listTable").datagrid('load',{
                queryStr:$("#queryStr").val(),
                groupType: 'activity'
            })
        }else{
            $("#listTable").datagrid('load',{
                verifyStatus:$("#verifyStatus").combobox('getValue'),
                queryStr:$("#queryStr").val(),
                groupType: 'activity'
            })
        }
    })
});

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/sysMessage.listByGroup.json');
    $('#listTable').datagrid({
        // type=0 即查询 普通公告类型数据
        url: url ,
        queryParams: {
            groupType: 'activity'
        },
        idField:'id',
        fit: true,
        pagination: true,
        pageSize: 30,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [
            [
                {
                    field:"ck",
                    checkbox:"true"
                },
                {
                    field: 'informTitle',
                    title: '标题'
                },
                {
                    field: 'createPerson',
                    title: '活动创建者',
                    align: 'center',
                    width:15
                },
                {
                    field: 'beginTime',
                    title: '生效时间',
                    align: 'center',
                    width:15
                },
                {
                    field: 'endTime',
                    title: '失效时间',
                    align: 'center',
                    width:15
                },{
                    field: 'customSign',
                    title: '首页推荐',
                    align: 'center',
                    width:10,
                    formatter:function (value, row, index) {
                        if(row.verifyStatus != 1){
                            return "--";
                        }
                        return value == 1 ? '推荐' : '不推荐';
                    }
                },{
                    field: 'verifyStatus',
                    title: '审核状态',
                    align: 'center',
                    width:10,
                    formatter:function (value, row, index) {
                        if(value == null || value == 3){
                            return '待审核';
                        }else{
                            return value == 1 ? '<font color=\'green\'>已通过</font>' : '<font color=\'red\'>未通过</font>';
                        }
                    }
                },{
                    field: 'context',
                    width:15,
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        var aEl ;
                        // if(row.verifyStatus == null){
                            aEl = '<a href=javascript:findDetail("{0}"); class="easyui-tooltip" title="详情">详情</a>' +
                                ' | <a href=javascript:verifyActivity("{0}"); class="easyui-tooltip" title="审核">审核</a>';
                        // }else{
                        //     aEl = '<a href=javascript:findDetail("{0}"); class="easyui-tooltip" title="详情">详情</a>';

                        // }
                        aEl += ' | <a href=javascript:deleteActivity("{0}"); class="easyui-tooltip" title="删除">删除</a>';
                        //格式化操作按钮
                        return $.string.format(aEl,row.id);
                    }
                }
            ]
        ]
    });
}

// 批量审核
function batchVerify(){
    var selections = $("#listTable").datagrid('getChecked');
    if(selections.length == 0){
        $.messager.alert('提示信息','并未勾选任何一条记录','info');
    }else{
        if(selections[0].verifyStatus != 0){
            $.messager.alert('提示信息','选择的内容已经审核完成','info');
        }else{
            var ids = "";
            for(var i = 0; i < selections.length;i++){
                if(i == selections.length-1){
                   ids += selections[i].id;
                }else{
                    ids += selections[i].id + "_";
                }
            }
            var title = "审核";
            var url = $.util.getRootPath() + '/page/operatorManager/activityVerify/verifyActivity.html?id='+ids;
            var dialog = $.easyui.showDialog({
                iconCls:"",
                title: title,
                width: 800,
                height: 460,
                maximizable: true,
                href: url,
                topMost: true,
                saveButtonText: '保存',
                onSave: function () {
                    verifyActivitySubForm(dialog);
                }
            });
        }
    }
}

// 批量删除
function batchDelte(){
    var selections = $("#listTable").datagrid('getChecked');
    if(selections.length == 0){
        $.messager.alert('提示信息','并未勾选任何一条记录','info');
    }else{
        if(selections[0].verifyStatus == 0){
            $.messager.alert('提示信息','选择的活动为未审核状态，不能进行删除操作','info');
        }else{
            var verifyStr = "确定要删除选择的 <b>"+selections.length+"</b> 项内容吗?";
            $.messager.confirm('确认对话框', verifyStr, function(r){
                if (r){
                    var ids = "";
                    for(var i = 0; i < selections.length;i++){
                        if(i == selections.length-1){
                            ids += selections[i].id;
                        }else{
                            ids += selections[i].id + "_";
                        }
                    }
                    $.util.get($.util.getRequestJson('/json/productManager.productDelete.json'),{id:ids},function(data){
                        if(data.success){
                            $('#listTable').datagrid('reload');
                        }
                    });
                }
            });
        }
    }
}

// 推荐到首页
function batchRecommend(){
    var selections = $("#listTable").datagrid('getChecked');
    if(selections.length == 0){
        $.messager.alert('提示信息','并未勾选任何一条记录','info');
    }else{
        var recognize = true;
        for(var i =0;i<selections.length;i++){
            if(selections[i].verifyStatus != 1){
                recognize = false;
            }
        }
        if(!recognize){
            $.messager.alert('提示信息','选择的活动不是审核通过状态，不能进行推荐到首页操作','info');
        }else{
            var verifyStr = "确定要将选择的 <b>"+selections.length+"</b> 项内容全部推荐到首页吗?";
            $.messager.confirm('确认对话框', verifyStr, function(r){
                if (r){
                    var ids = "";
                    for(var i = 0; i < selections.length;i++){
                        if(i == selections.length-1){
                            ids += selections[i].id;
                        }else{
                            ids += selections[i].id + "_";
                        }
                    }
                    $.util.get($.util.getRequestJson('/json/sysMessage.push2Index.json'),{ids:ids},function(data){
                        if(data.success){
                            $('#listTable').datagrid('reload');
                            $.easyui.messager.show('推荐成功');
                            $('#listTable').datagrid('clearChecked');
                        }
                    });
                }
            });
        }
    }
}

// 查看活动详情
function findDetail(id) {
    var title = "活动详情";
    var url = $.util.getRootPath() + '/page/operatorManager/notice/noticeDetail.html?id=' + id;
    //url = id ? url + '?id=2&typeId=' + typeId : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 600,
        height: 400,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton:false
    });
}

// 添加新公告或积分规则
function verifyActivity(id) {
    var title = id ? "审核" : "新增";
    var url = $.util.getRootPath() + '/page/operatorManager/activityVerify/verifyActivity.html';
    url = id ? url + '?id=' + id : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 800,
        height: 500,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            subForm(dialog);
        }
    });
}

// 审核活动操作方法
function verifyActivitySubForm(dialog){
    var url = $.util.getRootPath() + 'publishManage/checkActivity';
    var form = parent.$('#verifyProduct').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$('#verifyProduct').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

// 删除活动
function deleteActivity(activityId){
    $.messager.confirm('确认对话框',"确定要将该活动设置为删除状态吗？", function(r){
        if (r){
            $.util.get($.util.getRequestJson('/json/sysMessage.updateStatus.json'),{id:activityId,status:-1},function(data){
                if(data.success){
                    $('#listTable').datagrid('reload');
                }
            });
        }
    });
}

// 保存项目内容
function subForm(dialog) {
    var messageType = parent.$("#messageType").combobox('getValue');
    if (parent.$("#context").ueditor('getPlainTxt') == "") {
        parent.$.messager.alert('请先填写发布信息的内容');
        return;
    }
    var url = $.util.getRootPath() + '/sysMessage/save?verify=verify';
    var form = parent.$('#publishNotice').form('submit', {
        url: url,
        onSubmit: function (param) {
            return parent.$('#publishNotice').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}